% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

% This file is a template using the "beamer" package to create slides for a talk or presentation
% - Talk at a conference/colloquium.
% - Talk length is about 20min.
% - Style is ornate.

% MODIFIED by Jonathan Kew, 2008-07-06
% The header comments and encoding in this file were modified for inclusion with TeXworks.
% The content is otherwise unchanged from the original distributed with the beamer package.

\documentclass{beamer}


% Copyright 2004 by Till Tantau <tantau@users.sourceforge.net>.
%
% In principle, this file can be redistributed and/or modified under
% the terms of the GNU Public License, version 2.
%
% However, this file is supposed to be a template to be modified
% for your own needs. For this reason, if you use this file as a
% template and not specifically distribute it as part of a another
% package/program, I grant the extra permission to freely copy and
% modify this file as you see fit and even to delete this copyright
% notice. 


\mode<presentation>
{
  \usetheme{Warsaw}
  % or ...

  \setbeamercovered{transparent}
  % or whatever (possibly just delete it)
}


\usepackage{verbatim}
\usepackage[english]{babel}
% or whatever

\usepackage[utf8]{inputenc}
% or whatever

\usepackage{times}
\usepackage[T1]{fontenc}
% Or whatever. Note that the encoding and the font should match. If T1
% does not look nice, try deleting the line with the fontenc.


\title[Connect MongoDB by Programming] % (optional, use only with long paper titles)
{Connect MongoDB by Programming}


\author[Zhilei Ren] % (optional, use only with lots of authors)
{Zhilei Ren}
% - Give the names in the same order as the appear in the paper.
% - Use the \inst{?} command only if the authors have different
%   affiliation.

% This is only inserted into the PDF information catalog. Can be left
% out. 



% If you have a file called "university-logo-filename.xxx", where xxx
% is a graphic format that can be processed by latex or pdflatex,
% resp., then you can add a logo as follows:

% \pgfdeclareimage[height=0.5cm]{university-logo}{university-logo-filename}
% \logo{\pgfuseimage{university-logo}}



% Delete this, if you do not want the table of contents to pop up at
% the beginning of each subsection:


% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command: 

%\beamerdefaultoverlayspecification{<+->}


\begin{document}

\begin{frame}
  \titlepage
\end{frame}



% Structuring a talk is a difficult task and the following structure
% may not be suitable. Here are some rules that apply for this
% solution: 

% - Exactly two or three sections (other than the summary).
% - At *most* three subsections per section.
% - Talk about 30s to 2min per frame. So there should be between about
%   15 and 30 frames, all told.

% - A conference audience is likely to know very little of what you
%   are going to talk about. So *simplify*!
% - In a 20min talk, getting the main ideas across is hard
%   enough. Leave out details, even if it means being less precise than
%   you think necessary.
% - If you omit details that are vital to the proof/implementation,
%   just say so once. Everybody will be happy with that.
\begin{frame}
	\begin{block}{Preliminary}
		Credit goes to the authors, i.e.,
The example file from 

\scriptsize{https://github.com/mongodb/mongo-java-driver/blob/2.13.x/src/examples/example/QuickTour.java}

Slides from 

\scriptsize{http://mongodb.github.io/mongo-java-driver/2.13/getting-started/installation-guide/}


	\end{block}
	\pause
	\begin{alertblock}{Bash}
		\scriptsize
To compile, run 

\$ javac -classpath lib/bson-2.13.3.jar:lib/mongo-java-driver-2.13.3.jar example/MongoTut.java

To execute, run 

\$ java -classpath .:lib/bson-2.13.3.jar:lib/mongo-java-driver-2.13.3.jar example.MongoTut

	In Windows, change ':' to ';'
	\end{alertblock}
\end{frame}

\begin{frame}
	\begin{alertblock}{Java snippet}
		\scriptsize
package example;

import com.mongodb.BasicDBObject;

import com.mongodb.BulkWriteOperation;

import com.mongodb.BulkWriteResult;

import com.mongodb.Cursor;

import com.mongodb.DB;

import com.mongodb.DBCollection;

import com.mongodb.DBCursor;

import com.mongodb.DBObject;

import com.mongodb.MongoClient;

\ldots


	\end{alertblock}
\end{frame}

\begin{frame}
	\begin{block}{Connection}
		
To make a connection to a MongoDB, you need to have at the minimum, the name of a database to connect to. The database doesn’t have to exist -if it doesn’t, MongoDB will create it for you. Additionally, you can specify the server address and port when connecting. The following example shows four ways to connect to the database mydb on the local machine :
	\end{block}
	\pause
	\begin{alertblock}{Java snippet}
		\scriptsize
	MongoClient mongoClient = new MongoClient(); // or 
	
	MongoClient mongoClient = new MongoClient( "localhost" ); // or

	MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

	DB db = mongoClient.getDB( "mydb" );
	\end{alertblock}
\end{frame}

\begin{frame}
	\begin{block}{Authentication (Optional)}
MongoDB can be run in a secure mode where access to databases is controlled via authentication. When run in this mode, any client application must provide a list of credentials which will be used to authenticate against. In the Java driver, you simply provide the credentials when creating a MongoClient instance:
	\end{block}
	\pause
	\begin{alertblock}{Java snippet}
		\scriptsize
MongoCredential credential = MongoCredential.createCredential(userName, database, password);

MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential));
	\end{alertblock}
\end{frame}

\begin{frame}
	\begin{block}{Getting a Collection}
To get a collection to use, just specify the name of the collection to the getCollection(String collectionName) method:
\end{block}
	\pause
	\begin{alertblock}{Java snippet}
		\scriptsize
DBCollection coll = db.getCollection("testCollection");
\end{alertblock}

Once you have this collection object, you can now do things like insert data, query for data, etc
\end{frame}

\begin{frame}
	\begin{block}{Inserting a Document}
Once you have the collection object, you can insert documents into the collection. For example, lets make a little document that in JSON would be represented as

\{ "name" : "MongoDB", "type" : "database", "count" : 1, "info" : \{ x : 203, y : 102 \} \}

		Notice that the above has an “inner” document embedded within it. To do this, we can use the BasicDBObject class to create the document (including the inner document), and then just simply insert it into the collection using the insert() method.
	\end{block}
	\pause
	\begin{alertblock}{Java snippet}
		\scriptsize
		BasicDBObject doc = new BasicDBObject("name", "MongoDB")

        .append("type", "database")

        .append("count", 1)

        .append("info", new BasicDBObject("x", 203).append("y", 102));

		coll.insert(doc);
	\end{alertblock}
\end{frame}
\begin{frame}
	\begin{block}{Finding the First Document in a Collection Using findOne()}
To show that the document we inserted in the previous step is there, we can do a simple findOne() operation to get the first document in the collection. This method returns a single document (rather than the DBCursor that the find()’) operation returns), and it’s useful for things where there only is one document, or you are only interested in the first. You don’t have to deal with the cursor.

	\end{block}
	\pause
	\begin{alertblock}{Java snippet}
		\scriptsize
DBObject myDoc = coll.findOne();

System.out.println(myDoc);
	\end{alertblock}
\end{frame}

\begin{frame}
	\begin{block}{Using a Cursor to Get All the Documents}
In order to get all the documents in the collection, we will use the find() method. The find() method returns a DBCursor object which allows us to iterate over the set of documents that matched our query. So to query all of the documents and print them out :
	\end{block}
	\pause
	\begin{alertblock}{Java snippet}
		\scriptsize
DBCursor cursor = coll.find();

try \{

\ \ while(cursor.hasNext()) \{

\ \ \ \ System.out.println(cursor.next());

\ \ \}

\} finally \{

\ \ cursor.close();

\}
	\end{alertblock}
\end{frame}


\begin{frame}
	\begin{block}{Getting A Set of Documents With a Query}
We can use the query to get a set of documents from our collection. For example, if we wanted to get all documents where "i" > 50, we could write:
	\end{block}
	\pause
	\begin{alertblock}{Java snippet}
		\scriptsize
// find all where i > 50

query = new BasicDBObject("i", new BasicDBObject("\$gt", 50));

cursor = coll.find(query);

try \{

\ \ while (cursor.hasNext()) \{

\ \ \ \ System.out.println(cursor.next());

\ \ \}

\} finally \{

\ \ cursor.close();

\}

	\end{alertblock}
which should print the documents where i > 50.
\end{frame}

\end{document}


